/*********************************************************************
	\file :           os_sw_layers\bios\services\scheduler\Os_Tasks.c
	\brief:           Provide the configured tasks to the application
	\author:          Manuel Sandoval
	\version:         1.0
	\date:            Sat, 01, March 2014
*********************************************************************/


/******************************* Includes**********************************/

#include "Os_Tasks.h"
#include "Os_TaskM.h"


/******************************* Defines **********************************/
/*!< Macros for PORT A */
#define LED_Port    	PORTA
#define D0         		1
#define D1         		2
#define D2         		4
#define D3         		8
#define D4         		16
#define D5         		32
#define D6         		64
#define D7         		128


/*!< Macros for PORT B */
#define PortB    	PORTB
#define B0         		1
#define B1         		2
#define B2         		4
#define B3         		8
#define B4         		16
#define B5         		32
#define B6         		64
#define B7         		128


/*!< Enable LED ON*/
#define LED_ON(BitMask)                     (LED_Port |= (u8)(BitMask))
/*!< Disable LED OFF*/
#define LED_OFF(BitMask)                    (LED_Port &= ~(u8)(BitMask))
/*!< Toggle LED */
#define LED_TOGGLE(BitMask)                 (LED_Port ^= (u8)(BitMask)) 

/*!< Enable PIN ON*/
#define PIN_ON(BitMask)                     (PortB |= (u8)(BitMask))
/*!< Disable PIN OFF*/
#define PIN_OFF(BitMask)                    (PortB &= ~(u8)(BitMask))
/*!< Toggle PIN */
#define PIN_TOGGLE(BitMask)                 (PortB ^= (u8)(BitMask)) 


#define DELAY    1000  

/******************************* Enumerations******************************/


/******************************* Structures *******************************/


/******************************* Global Variables **********************************/


/******************************* Prototypes ********************************/
void vfnDelay(u32 time);


/******************************* Functions ********************************/

/***************************************************************************
*
*   FUNCTION NAME: Task_1ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 1ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/

TASK(Task_1ms)
{
    u16 u16Iterator;

    /*!< Led ON*/
    LED_ON(D0);
    for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
	  {

	  }

    /*!< Led OFF*/
    LED_OFF(D0);

	/* Task Code */
	TerminateTask();
} /* Task_1ms */


/***************************************************************************
*
*   FUNCTION NAME: Task_4ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 4ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_4ms)
{
    u16 u16Iterator;

    /*!< Led ON*/
    LED_ON(D1);

    for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
	  {

	  }

    /*!< Led OFF*/
    LED_OFF(D1);

	/* Task Code */
	TerminateTask();
} /* Task_4ms */


/***************************************************************************
*
*   FUNCTION NAME: Task_8ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 8ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_8ms)
{
    u16 u16Iterator;

    /*!< Led ON*/
    LED_ON(D2);

    for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
	  {

	  }

    /*!< Led OFF*/
    LED_OFF(D2);

	/* Task Code */
	TerminateTask();
} /* Task_8ms */



/***************************************************************************
*
*   FUNCTION NAME: Task_16ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 16ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_16ms)
{
  u16 u16Iterator;

  /*!< PIN ON*/
  PIN_ON(B0);
  for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
  {

  }

  /*!< PIN OFF*/
  PIN_OFF(B0);

	/* Task Code */
	TerminateTask();
} /* Task_16ms */


/***************************************************************************
*
*   FUNCTION NAME: Task_32ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 32ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_32ms)
{
  u16 u16Iterator;

  /*!< PIN ON*/
  PIN_ON(B1);
  for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
  {

  }

  /*!< PIN OFF*/
  PIN_OFF(B1);
    
	/* Task Code */
	TerminateTask();
} /* Task_32ms */


/***************************************************************************
*
*   FUNCTION NAME: Task_64ms
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a 64ms task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_64ms)
{
  u16 u16Iterator;

  /*!< PIN ON*/
  PIN_ON(B2);
  for(u16Iterator = 0; u16Iterator < DELAY; u16Iterator++)
  {

  }

  /*!< PIN OFF*/
  PIN_OFF(B2);
  
	/* Task Code */
	TerminateTask();
} /* Task_64ms */


/***************************************************************************
*
*   FUNCTION NAME: Task_PITCH1
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a PITCH1 task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_PITCH1)
{
  /* do something */
  PIN_ON(B2);
  vfnDelay(50);
  PIN_OFF(B2);
  /* Task Code */
  TerminateTask();
}/* Task_PITCH1 */

/***************************************************************************
*
*   FUNCTION NAME: Task_PITCH2
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a PITCH2 task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_PITCH2)
{
  /* do something */
  PIN_TOGGLE(B3);
  /* Task Code */
  TerminateTask();
}/* Task_PITCH2 */


/***************************************************************************
*
*   FUNCTION NAME: Task_PP0
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a PP0 task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_PP0) 
{
  /* do something */
	PIN_TOGGLE(B4);
  /* Task Code */
  TerminateTask();
}/* Task_PP0 */


/***************************************************************************
*
*   FUNCTION NAME: Task_PP1
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a PP1 task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
TASK(Task_PP1) 
{
  /* do something */
	PIN_TOGGLE(B5);
  /* Task Code */
  TerminateTask();
}/* Task_PP1 */

/***************************************************************************
*
*   FUNCTION NAME: SchM_Task_Background
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a SchM_Task_Background task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
void SchM_Task_Background( void )
{

	SchM_Task_vfnRotabit();
}/* SchM_Task_Background */


/***************************************************************************
*
*   FUNCTION NAME: SchM_Task_vfnRotabit
*
***************************************************************************/
/**
* @brief Provides a single interface to Activate a SchM_Task_vfnRotabit task.
*
*
* @param[in] none.
* @param[in] none.
*
* @return void.
*
* None.
*
***************************************************************************/
/* SchM_Task_vfnRotabit */
void SchM_Task_vfnRotabit(void)
{
  /* Configurations */
  const u8 cu8NumLeds = 4;
  const u32 cu32Delay = 70000;
  
  static u8 u8Rotabit = 0;  
  static u32 u32Counter = 0;
  
  u32Counter++;
  
  if(cu32Delay == u32Counter)
  {
    u32Counter = 0;
    
    LED_OFF(1<<u8Rotabit);
    
    u8Rotabit++;
    
    if(u8Rotabit >= cu8NumLeds)
    {
      u8Rotabit = 0;
    } 
    
    LED_ON(1<<u8Rotabit);   
  }
  
} /* SchM_Task_vfnRotabit */


void vfnDelay(u32 time)
{
 while(time--);
}


/** @} *//* End of Scheduler Tasks Group */


/*********************************************************************
	File	: os_sw_layers\bios\services\scheduler\Os_Tasks.c
*********************************************************************/
